using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace Vin.Extension.ImageSharp.Extension;

public static class ImageExtension
{
    /// <summary>
    /// 重置图片大小
    /// </summary>
    public static Image Resize(this Image image, int width, int height)
    {
        return image.Clone(ctx => ctx.Resize(width, height));
    }

    /// <summary>
    /// 将图片转换为圆形
    /// </summary>
    public static Image ToCircle(this Image image, Size size)
    {
        image = image.Resize(size.Width, size.Height);
        var result = new Image<Rgba32>(size.Width, size.Height);
        // 创建一个圆形路径
        IPath circlePath = new EllipsePolygon(size.Width / 2f, size.Height / 2f, size.Width / 2f);
        // 填充圆形路径
        result.Mutate(ctx =>
        {
            ctx.Fill(Color.Transparent);
            ctx.Fill(Color.White, circlePath);
            ctx.DrawImage(image, new Rectangle(0, 0, size.Width, size.Height), PixelColorBlendingMode.Add,
                PixelAlphaCompositionMode.SrcIn, 1);
        });
        return result;
    }

    /// <summary>
    /// 将图片转换为圆角矩形
    /// </summary>
    public static Image ToCorner(this Image image, Size size, float cornerRadius)
    {
        image = image.Resize(size.Width, size.Height);

        var width = size.Width;
        var height = size.Height;

        // 确保圆角半径不超过短边的一半
        cornerRadius = Math.Min(cornerRadius, Math.Min(width, height) / 2f);

        var result = new Image<Rgba32>(width, height);

        // 填充完全透明背景
        result.Mutate(x => x.Fill(Color.Transparent));

        // 创建圆角矩形路径
        var roundedRect = BuildCorners(width, height, cornerRadius);

        // 在遮罩上绘制白色圆角矩形（完全不透明）
        result.Mutate(x => x.Fill(Color.White, roundedRect));

        // 填充圆角矩形路径
        result.Mutate(ctx =>
        {
            ctx.DrawImage(image, new Rectangle(0, 0, size.Width, size.Height), PixelColorBlendingMode.Add,
                PixelAlphaCompositionMode.SrcOut, 1);
        });

        return result;
    }

    private static PathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
    {
        // First create a square
        var rect = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);

        // Then cut out of the square a circle so we are left with a corner
        IPath cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));

        // Corner is now a corner shape positions top left
        // let's make 3 more positioned correctly, we can do that by translating the original around the center of the image.

        float rightPos = imageWidth - cornerTopLeft.Bounds.Width + 1;
        float bottomPos = imageHeight - cornerTopLeft.Bounds.Height + 1;

        // Move it across the width of the image - the width of the shape
        IPath cornerTopRight = cornerTopLeft.RotateDegree(90).Translate(rightPos, 0);
        IPath cornerBottomLeft = cornerTopLeft.RotateDegree(-90).Translate(0, bottomPos);
        IPath cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);

        return new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight);
    }
}